package bank;

import bank.User;

import java.util.Scanner;

/**
 * 银行类
 */
public class Bank {
    Scanner input = new Scanner(System.in);// Bank的属性

    User[] users = new User[5];//存储所有银行用户的信息 { 0x11223344 , 0x22222222 , 0x33333333 , 0x44444444 , null }

    int size = 4;//有效元素个数 { 0x11223344 , null , 0x33333333 , 0x44444444 , null }

    /**
     * 完成用户信息的初始化
     */
    public void initial() {
        User user = new User();// 0x11223344
        user.setCardNo("6222020200001234567");
        user.setPassword("123456");
        user.setUsername("tom123");
        user.setIdentityCard("110101199009096789");
        user.setPhone("13909094455");
        user.setBalance(3000.0);

        users[0] = user;

        User user2 = new User("6222020200001237654", "123456", "marry", "110101199009106666", "13988887777", 5000.0);// 0x22222222
        users[1] = user2;// 0x22222222

        users[2] = new User("6222020200005555666", "123456", "annie", "110101199009108888", "13988889999", 4000.0);// 0x33333333

        users[3] = new User("6222020200005555443", "123456", "eric", "110101199009104433", "13988881234", 7000.0);// 0x44444444
    }

    /**
     * 欢迎菜单（包含开户、登录功能的调用）
     */
    public void welcomeMenu() {

        System.out.println("-------------欢迎使用ATM自动银行系统-------------");
        System.out.println("1.开户  2.登录");
        System.out.println("请输入操作编号：");
        int choice = input.nextInt();

        switch(choice) {
            case 1:
                this.showAll();
                this.register();//调用开户
                this.showAll();
                break;
            case 2:
                this.login();//调用登录
                break;
        }
    }

    /**
     * 接收用户名和密码，并调用check方法验证
     */
    public void login() {

        // 用户输入卡号、密码
        System.out.println("请输入卡号：");
        String no = input.next();

        System.out.println("请输入密码：");
        String pwd = input.next();

        User myAccount = this.check(no,pwd);//如果验证成功

        if(myAccount != null) {
            this.showMenu(myAccount);//调用业务功能菜单
        }else {
            System.out.println("卡号或密码不正确");
        }
    }

    /**
     * 验证用户名和密码是否正确
     */
    public User check(String no , String pwd) {
        // 验证卡密是否与开户时的信息一致
        for (int i = 0; i < users.length; i++) {
            if (users[i] != null) { // 非空判断
                if (no.equals(users[i].getCardNo()) && pwd.equals(users[i].getPassword())) {
                    return users[i];//查找到与自己卡号密码匹配的“完整账户”（身份证号、手机号、余额）
                }
            }
        }

        return null;
    }

    /**
     * 开户功能
     */
    public void register() {//insert

        //做数组长度的判断，是否需要扩容
        if(users.length == size) {
            //this.expand();
        }

        System.out.println("请输入密码：");
        String pwd = input.next();

        System.out.println("请输入姓名：");
        String name = input.next();

        System.out.println("请输入身份证号：");
        String id = input.next();

        System.out.println("请输入电话：");
        String phone = input.next();

        //固定前缀（622202020000）+ 自动生成的随机数（7位）
        String prefix = "622202020000";

        //随机后缀：Math.random(); 获取一个大于0小于1的整数（0421543.756894768954）
        int suffix = (int)(Math.random() * 1000000) + 1000000;

        String no = prefix + suffix;//生成的卡号


        //新的账户信息
        User user = new User();
        user.setCardNo(no);
        user.setPassword(pwd);
        user.setUsername(name);
        user.setPhone(phone);
        user.setIdentityCard(id);

        //将新账户信息，存入用户数组
        users[size] = user;

        size++;
    }

    /**
     * 取款功能
     */
    public void withdrawal(User myAccount) {

        System.out.println("请输入取款金额：");
        double money = input.nextDouble();

        if(money > 0 && money < myAccount.getBalance() ) {//myAccount.getBalance();
            //正常取款

            double currentBalance = myAccount.getBalance() - money;

            myAccount.setBalance(currentBalance);

            System.out.println("取款成功，当前余额为：" + myAccount.getBalance());

        }else {
            System.out.println("余额不足！");
        }

    }

    /**
     * 转账功能
     */
    public void transfer(User myAccount) {
        //1.判断对方的卡号和姓名 User toUser = this.checkToAccount("62220000xxxxxxxx" , "marry");
        //2.输入转账的金额money
        //3.判断自己的余额
        //4.自己账户的余额-money
        //5.对方账户的余额+money
    }

    /**
     * 展示银行业务功能的菜单
     */
    public void showMenu(User myAccount) {
        int choice;
        while (true) {
            System.out.println("2.存款  3.取款  4.转账  5.查询余额  6.修改密码  7.修改预留手机号码  8.注销账号  0.退出");
            System.out.println("-------------------------------------");
            System.out.println("请输入操作编号：");
            choice = input.nextInt();

            switch (choice) {
                case 2:
                    System.out.println("执行存款");
                    break;
                case 3:
                    this.withdrawal(myAccount);//调用取款功能
                    break;
                case 4:
                    this.transfer(myAccount);//调用转账功能
                    break;
                case 5:
                    System.out.println("执行查询余额");
                    break;
                case 6:
                    System.out.println("执行修改密码");
                    break;
                case 7:
                    System.out.println("执行修改预留手机号码");
                    break;
                case 8:
                    System.out.println("执行注销账号");
                    break;
                case 0:
                    System.out.println("执行退出");
                    return;// 退出整个方法
                default:
                    System.out.println("输入有误，请重新输入！");
            }
        }
    }


    public void showAll() {
        for(int i = 0 ; i < size ; i++) {
            System.out.println( users[i].getCardNo() +"\t"+ users[i].getUsername() +"\t"+ users[i].getBalance() );
        }
    }
}
